package problems.solved;

import problems.Problem;

public class Problem40 extends Problem<Integer> {
	private static final int TARGET = 1000000;

	public static void main(String[] args) {
		Problem40 problem40 = new Problem40();
		problem40.runTimed();
		problem40.printResult();
	}

	@Override
	public void run() {
		int[] array = {100, 1000, 10000, 100000, TARGET};
		int arrayIndex = 0;

		int result = 1; // indices 1 and 10
		
		int next = 11;
		int index = 12;
		
		while (index < TARGET) {
			int prev = index;
			final String current = Integer.toString(next++);
			index += current.length();
			
			final int v = array[arrayIndex];
			if (prev <= v && index > v) {
				final int c = current.charAt(v - prev) - 48;
				System.out.println("d" + v + " = " + c);
				result *= c;
				arrayIndex++;
			}
		}
		
		setResult(result);
	}
}
